/**
 * Title :	jFamilyTree generator
 * Description: jFamilyTree generator
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.generator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import jFamilyTree.Messages;

/**
 * Menu bar
 * 
 * @author Bernard Bou
 */
public class Menubar extends JMenuBar
{
	private static final long serialVersionUID = 1L;

	// D A T A

	/**
	 * Controller (command sink)
	 */
	private Controller theController;

	// C O N S T R U C T O R

	/**
	 * Constructor
	 */
	public Menubar()
	{
		initialize();
	}

	/**
	 * Initialize
	 */
	private void initialize()
	{
		JMenuItem thisOpenMenuItem = makeItem(Messages.getString("Menubar.110"), "open.png", Controller.Code.OPEN, KeyStroke.getKeyStroke('O', InputEvent.CTRL_DOWN_MASK)); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisOpenUrlMenuItem = makeItem(Messages.getString("Menubar.120"), "openurl.png", Controller.Code.OPENURL, null); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisImportXslMenuItem = makeItem(Messages.getString("Menubar.130"), "import.png", Controller.Code.IMPORTXSL, null); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisImportProviderMenuItem = makeItem(Messages.getString("Menubar.131"), "importprovider.png", Controller.Code.IMPORTPROVIDER, null); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisNewMenuItem = makeItem(Messages.getString("Menubar.140"), "newdoc.png", Controller.Code.NEW, KeyStroke.getKeyStroke('N', InputEvent.CTRL_DOWN_MASK)); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisSaveMenuItem = makeItem(Messages.getString("Menubar.150"), "save.png", Controller.Code.SAVE, KeyStroke.getKeyStroke('S', InputEvent.CTRL_DOWN_MASK)); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisSaveAsMenuItem = makeItem(Messages.getString("Menubar.151"), "saveas.png", Controller.Code.SAVEAS, null); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisExportMenuItem = makeItem(Messages.getString("Menubar.160"), "export.png", Controller.Code.EXPORT, null); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisExportSettingsMenuItem = makeItem(Messages.getString("Menubar.161"), null, Controller.Code.EXPORTSETTINGS, null); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisMakeSiteMenuItem = makeItem(Messages.getString("Menubar.170"), "sitemake.png", Controller.Code.MAKESITE, null); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisRunSiteMenuItem = makeItem(Messages.getString("Menubar.171"), "siterun.png", Controller.Code.RUNSITE, null); //$NON-NLS-1$ //$NON-NLS-2$

		JMenuItem thisNewElementMenuItem = makeItem(Messages.getString("Menubar.200"), "new.png", Controller.Code.NEWELEMENT, KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0)); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisRemoveElementMenuItem = makeItem(Messages.getString("Menubar.210"), "delete.png", Controller.Code.REMOVEELEMENT, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisNormalizeIdsMenuItem = makeItem(Messages.getString("Menubar.220"), null, Controller.Code.NORMALIZEIDS, null); //$NON-NLS-1$
		JMenuItem thisListImagesMenuItem = makeItem(Messages.getString("Menubar.230"), null, Controller.Code.LISTIMAGES, null); //$NON-NLS-1$
		JMenuItem thisListLinksMenuItem = makeItem(Messages.getString("Menubar.231"), null, Controller.Code.LISTLINKS, null); //$NON-NLS-1$
		JMenuItem thisListMountsMenuItem = makeItem(Messages.getString("Menubar.232"), null, Controller.Code.LISTMOUNTS, null); //$NON-NLS-1$
		JMenuItem thisListIdsMenuItem = makeItem(Messages.getString("Menubar.233"), null, Controller.Code.LISTIDS, null); //$NON-NLS-1$

		JMenuItem thisSettingsMenuItem = makeItem(Messages.getString("Menubar.300"), "settings.png", Controller.Code.SETTINGS, null); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisBaseMenuItem = makeItem(Messages.getString("Menubar.310"), "site.png", Controller.Code.SETTINGSBASE, null); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisUrlMenuItem = makeItem(Messages.getString("Menubar.320"), "url.png", Controller.Code.SETTINGSURL, null); //$NON-NLS-1$ //$NON-NLS-2$
		JCheckBoxMenuItem thisjFamilyTreeRendererMenuItem = makeCheckboxItem(Messages.getString("Menubar.330"), null, Controller.Code.OPTIONTREEBOLICRENDERER, null, MainFrame.hasjFamilyTreeRendering); //$NON-NLS-1$
		JCheckBoxMenuItem thisValidateMenuItem = makeCheckboxItem(Messages.getString("Menubar.331"), null, Controller.Code.OPTIONVALIDATEXML, null, Controller.validate); //$NON-NLS-1$
		JCheckBoxMenuItem thisFocusParentMenuItem = makeCheckboxItem(Messages.getString("Menubar.333"), null, Controller.Code.OPTIONFOCUSPARENT, null, jFamilyTree.generator.tree.Tree.focusParent); //$NON-NLS-1$

		JMenuItem thisDtdMenuItem = makeItem(Messages.getString("Menubar.410"), "dtd.png", Controller.Code.DTD, null); //$NON-NLS-1$ //$NON-NLS-2$

		JMenuItem thisAboutMenuItem = makeItem(Messages.getString("Menubar.911"), "about.png", Controller.Code.ABOUT, null); //$NON-NLS-1$ //$NON-NLS-2$
		JMenuItem thisHelpMenuItem = makeItem(Messages.getString("Menubar.910"), "help.png", Controller.Code.HELP, null); //$NON-NLS-1$ //$NON-NLS-2$

		JMenu thisFilesMenu = new JMenu();
		thisFilesMenu.setText(Messages.getString("Menubar.1")); //$NON-NLS-1$
		thisFilesMenu.add(thisNewMenuItem);
		thisFilesMenu.add(thisOpenMenuItem);
		thisFilesMenu.add(thisOpenUrlMenuItem);
		thisFilesMenu.add(thisImportXslMenuItem);
		thisFilesMenu.add(thisImportProviderMenuItem);
		thisFilesMenu.addSeparator();
		thisFilesMenu.add(thisSaveMenuItem);
		thisFilesMenu.add(thisSaveAsMenuItem);
		thisFilesMenu.add(thisExportMenuItem);
		thisFilesMenu.add(thisExportSettingsMenuItem);
		thisFilesMenu.addSeparator();
		thisFilesMenu.add(thisMakeSiteMenuItem);
		thisFilesMenu.add(thisRunSiteMenuItem);

		JMenu thisElementsMenu = new JMenu();
		thisElementsMenu.setText(Messages.getString("Menubar.2")); //$NON-NLS-1$
		thisElementsMenu.add(thisNewElementMenuItem);
		thisElementsMenu.add(thisRemoveElementMenuItem);
		thisElementsMenu.addSeparator();
		thisElementsMenu.add(thisNormalizeIdsMenuItem);
		thisElementsMenu.addSeparator();
		thisElementsMenu.add(thisListImagesMenuItem);
		thisElementsMenu.add(thisListLinksMenuItem);
		thisElementsMenu.add(thisListMountsMenuItem);
		thisElementsMenu.add(thisListIdsMenuItem);

		JMenu thisOptionsMenu = new JMenu();
		thisOptionsMenu.setText(Messages.getString("Menubar.3")); //$NON-NLS-1$
		thisOptionsMenu.add(thisSettingsMenuItem);
		thisOptionsMenu.add(thisBaseMenuItem);
		thisOptionsMenu.add(thisUrlMenuItem);
		thisOptionsMenu.addSeparator();
		thisOptionsMenu.add(thisjFamilyTreeRendererMenuItem);
		thisOptionsMenu.add(thisValidateMenuItem);
		thisOptionsMenu.add(thisFocusParentMenuItem);

		JMenu thisHelpMenu = new JMenu();
		thisHelpMenu.setText(Messages.getString("Menubar.9")); //$NON-NLS-1$
		thisHelpMenu.add(thisAboutMenuItem);
		thisHelpMenu.add(thisHelpMenuItem);

		JMenu thisToolsMenu = new JMenu();
		thisToolsMenu.setText(Messages.getString("Menubar.4")); //$NON-NLS-1$
		thisToolsMenu.add(thisDtdMenuItem);

		this.add(thisFilesMenu);
		this.add(thisElementsMenu);
		this.add(thisOptionsMenu);
		this.add(thisToolsMenu);
		this.add(thisHelpMenu);
	}

	/**
	 * Set controller
	 * 
	 * @param thisController
	 *        controller
	 */
	public void setController(Controller thisController)
	{
		theController = thisController;
	}

	/**
	 * Make menu item
	 * 
	 * @param thisText
	 *        text
	 * @param thisImage
	 *        image
	 * @param thisCommand
	 *        command code
	 * @param thisAcceleratorKey
	 *        accelerator key
	 * @return menu item
	 */
	private JMenuItem makeItem(String thisText, String thisImage, final Controller.Code thisCommand, KeyStroke thisAcceleratorKey)
	{
		JMenuItem thisItem = new JMenuItem();
		thisItem.setText(thisText);
		if (thisImage != null)
			thisItem.setIcon(new ImageIcon(Menubar.class.getResource("images/" + thisImage))); //$NON-NLS-1$
		if (thisAcceleratorKey != null)
			thisItem.setAccelerator(thisAcceleratorKey);
		thisItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				theController.execute(thisCommand, 0);
			}
		});
		return thisItem;
	}

	/**
	 * Make menu item
	 * 
	 * @param thisText
	 *        text
	 * @param thisImage
	 *        image
	 * @param thisCommand
	 *        command code
	 * @param thisAcceleratorKey
	 *        accelerator key
	 * @return menu item
	 */
	private JCheckBoxMenuItem makeCheckboxItem(String thisText, String thisImage, final Controller.Code thisCommand, KeyStroke thisAcceleratorKey, boolean thisState)
	{
		JCheckBoxMenuItem thisItem = new JCheckBoxMenuItem();
		thisItem.setText(thisText);
		thisItem.setSelected(thisState);
		if (thisImage != null)
			thisItem.setIcon(new ImageIcon(Menubar.class.getResource("images/" + thisImage))); //$NON-NLS-1$
		if (thisAcceleratorKey != null)
			thisItem.setAccelerator(thisAcceleratorKey);
		thisItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				theController.execute(thisCommand, 0);
			}
		});
		return thisItem;
	}
}